<?php
/*
 * 列表参数
*/
namespace App\Validation;

use Phalcon\Validation\Validator\PresenceOf;
use Phalcon\Validation\Validator\Numericality;

class ListValidation extends BaseValidation
{
    public function initialize()
    {

		$this->add(
			"p",
			new Numericality(
				  [
					  "message" => ":field is not numeric",
				  ]
			)
		);
		
		$this->add(
			"page_num",
			new Numericality(
				  [
					  "message" => ":field is not numeric",
				  ]
			)
		);
		
		$this->add(
			"host_id",
			new Numericality(
				  [
					  "message" => ":field is not numeric",
				  ]
			)
		);

		$this->add(
			"city_id",
			new Numericality(
				  [
					  "message" => ":field is not numeric",
				  ]
			)
		);

		$this->add(
			"cate_id",
			new Numericality(
				  [
					  "message" => ":field is not numeric",
				  ]
			)
		);

		$this->add(
			"type_id",
			new Numericality(
				  [
					  "message" => ":field is not numeric",
				  ]
			)
		);

		$this->add(
			"group_id",
			new Numericality(
				  [
					  "message" => ":field is not numeric",
				  ]
			)
		);

		$this->add(
			"target_id",
			new PresenceOf(
				  [
					  "message" => ":field is required",
					  "allowEmpty" => true,
				  ]
			)
		);

		$this->add(
			"is_reco",
			new PresenceOf(
				  [
					  "message" => ":field is required",
					  "allowEmpty" => true,
				  ]
			)
		);

		$this->add(
			"title",
			new PresenceOf(
				  [
					  "message" => ":field is required",
					  "allowEmpty" => true,
				  ]
			)
		);	
		
    }
	
	//验证之前
	public function beforeValidation($data, $entity, $messages)
    {
    	parent::beforeValidation($data, $entity, $messages);

    	$data->is_reco = property_exists($data,'is_reco') ? $data->is_reco : 0;
		$data->host_id = property_exists($data,'host_id') ? $data->host_id : 0;
		$data->city_id = property_exists($data,'city_id') ? $data->city_id : 0;
		$data->type_id = property_exists($data,'type_id') ? $data->type_id : 0;
		$data->cate_id = property_exists($data,'cate_id') ? $data->cate_id : 0;
		$data->group_id = property_exists($data,'group_id') ? $data->group_id : 0;
		$data->target_id = property_exists($data,'target_id') ? $data->target_id : 0;
		
        return true;
    }
	
}